package com.kll.carolinehmibiz.presenter

import android.content.Context
import com.kll.carolinehmibiz.base.net.bean.BaseHttpResult
import com.kll.carolinehmibiz.base.net.rx.NetObserver
import com.kll.carolinehmibiz.model.AccountModel
import com.kll.carolinehmibiz.model.OperateListener
import com.kll.carolinehmibiz.net.CarolineNetHelper
import com.kll.carolinehmibiz.net.bean.EmptyResponse
import com.kll.carolinehmibiz.net.bean.LoginResponse
import com.kll.carolinehmibiz.net.bean.RefreshTokenResponse


/**
 * ====================================================
 * Created by Ziliang Cai on 2021/12/8 20:30
 * <a href="mailto:752777456@qq.com">Contact me</a>
 * ====================================================
 */
class LoginPresenter: BasePresenter {


    private constructor(context: Context): super(context){

    }

    companion object {

        private var mInstance: LoginPresenter? = null

        fun getInstance(context: Context): LoginPresenter {
            if(mInstance == null)
                mInstance = LoginPresenter(context)
            return mInstance!!
        }

    }

    fun logout(){
        AccountModel.getInstance(mContext).clear()
    }

    fun isLogin():Boolean{
        return AccountModel.getInstance(mContext).getUserToken() != null
    }

    fun login(mobile: String,  verifyCode: String, listener: OperateListener) {

        CarolineNetHelper.getInstance(mContext).login(object:
            NetObserver<BaseHttpResult<LoginResponse>>() {
            override fun onSuccess(t: BaseHttpResult<LoginResponse>) {

                //  保存登录态数据
                AccountModel.getInstance(mContext).saveLoginInfo(t.data)

                // 回调登录监听
                listener.onSuccess(t.status, t.data)
            }

            override fun onFail(code: Int, message: String) {
                listener.onFail(code, message)
            }
        }, verifyCode, mobile)
    }

    /**
     * 重置密码
     */
    fun resetPwd(mobile: String, verifyCode: String, pwd: String, listener: OperateListener) {
        CarolineNetHelper.getInstance(mContext).resetPwd(object:
            NetObserver<BaseHttpResult<EmptyResponse>>() {
            override fun onSuccess(t: BaseHttpResult<EmptyResponse>) {
                // 退出登录
                AccountModel.getInstance(mContext).clear()
                listener.onSuccess(t.status, t.data)
            }

            override fun onFail(code: Int, message: String) {
                listener.onFail(code, message)
            }

        }, mobile, verifyCode, pwd)
    }

    /**
     * 获取验证码
     */
    fun getVerifyCode(mobile: String, listener: OperateListener) {
        CarolineNetHelper.getInstance(mContext).getVerifyCode(object:
            NetObserver<BaseHttpResult<ArrayList<EmptyResponse>>>() {
            override fun onSuccess(t: BaseHttpResult<ArrayList<EmptyResponse>>) {
                listener.onSuccess(t.status, t.data)
            }

            override fun onFail(code: Int, message: String) {
                listener.onFail(code, message)
            }
        }, mobile)
    }

    fun register(sn: String, mobile: String, verifyCode: String, listener: OperateListener) {
        CarolineNetHelper.getInstance(mContext).register(object: NetObserver<BaseHttpResult<Boolean>>(){
            override fun onSuccess(t: BaseHttpResult<Boolean>) {
                listener.onSuccess(t.status, t.data)
            }

            override fun onFail(code: Int, message: String) {
                listener.onFail(code, message)
            }

        }, sn, mobile, verifyCode)
    }


    /**
     * 刷新token
     */
    fun refreshToken(listener: OperateListener) {
        CarolineNetHelper.getInstance(mContext).refreshToken(object : NetObserver<BaseHttpResult<RefreshTokenResponse>>() {
            override fun onSuccess(t: BaseHttpResult<RefreshTokenResponse>) {
                listener.onSuccess(t.status, t.data)
            }

            override fun onFail(code: Int, message: String) {
                listener.onFail(code, message)
            }

        })
    }

}